﻿//###HEADER###
//Title: RevitPlus
//Description of Program: Utilzing Revit geometry and properties, user is able to perform an EnergyPlus calculation within Revit.
//Class:GetMaterialProperties
//Description of Class:   Obtains all floor types, wall types, and roof types. Catches if none are given and uses a default value.
//       
//By: Steve Chick & Alexander Bernstein
//Date: 12/18/2012
//Version: 001

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

namespace Revit_Addin1
{
    //Define Class
    public class GetMaterialProperties
    {
        public void getmaterials(ExternalCommandData commandData)
        {
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;

            //Clear all previous values in the list
            StoredVariables.FloorTypes.Clear();
            StoredVariables.FloorRoughness.Clear();
            StoredVariables.FloorThermalResistance.Clear();
            StoredVariables.FloorThermalAbsorptance.Clear();
            StoredVariables.FloorSolarAbsorptance.Clear();
            StoredVariables.FloorVisibleAbsorptance.Clear();

            StoredVariables.WallTypes.Clear();
            StoredVariables.WallRoughness.Clear();
            StoredVariables.WallThermalResistance.Clear();
            StoredVariables.WallThermalAbsorptance.Clear();
            StoredVariables.WallSolarAbsorptance.Clear();
            StoredVariables.WallVisibleAbsorptance.Clear();

            StoredVariables.RoofTypes.Clear();
            StoredVariables.RoofRoughness.Clear();
            StoredVariables.RoofThermalResistance.Clear();
            StoredVariables.RoofThermalAbsorptance.Clear();
            StoredVariables.RoofSolarAbsorptance.Clear();
            StoredVariables.RoofVisibleAbsorptance.Clear();

            //Gather all floortypes in the document
            foreach (FloorType floortype in doc.FloorTypes)
            {
                StoredVariables.FloorTypes.Add(floortype.Name.ToString());
                //Value does not exist in Revit. Apply default value to start
                StoredVariables.FloorRoughness.Add("MediumRough");

                //Try to grab the specified parameter
                try
                {
                    //If Value is 0 default to a standard value of R4
                    if (floortype.ThermalProperties.ThermalResistance == 0)
                    {
                        StoredVariables.FloorThermalResistance.Add(0.75);
                    }
                    else
                    {
                        StoredVariables.FloorThermalResistance.Add(floortype.ThermalProperties.ThermalResistance);
                    }
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.FloorThermalResistance.Add(0.75);
                }
                //Try to grab the specified parameter
                try
                {
                    StoredVariables.FloorThermalAbsorptance.Add(floortype.ThermalProperties.Absorptance);
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.FloorThermalAbsorptance.Add(0.1);
                }

                //Values do not exists in Revit. Apply default values to start
                StoredVariables.FloorSolarAbsorptance.Add(0.6);
                StoredVariables.FloorVisibleAbsorptance.Add(0.6);
            }

            //Gather all walltypes in the document
            foreach (WallType walltype in doc.WallTypes)
            {
                StoredVariables.WallTypes.Add(walltype.Name.ToString());
                //Value does not exist in Revit. Apply default value to start
                StoredVariables.WallRoughness.Add("MediumRough");
                //Try to grab the specified parameter
                try
                {
                    //If Value is 0 default to a standard value of R4
                    if (walltype.ThermalProperties.ThermalResistance == 0)
                    {
                        StoredVariables.WallThermalResistance.Add(0.75);
                    }
                    else
                    {
                        StoredVariables.WallThermalResistance.Add(walltype.ThermalProperties.ThermalResistance);
                    }
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.WallThermalResistance.Add(0.75);
                }
                //Try to grab the specified parameter
                try
                {
                    StoredVariables.WallThermalAbsorptance.Add(walltype.ThermalProperties.Absorptance);
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.WallThermalAbsorptance.Add(0.1);
                }
                //Try to grab the specified parameter
                try
                {
                    StoredVariables.WallFunction.Add(walltype.get_Parameter("Function").AsInteger());
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.WallFunction.Add(1);
                }
                //Values do not exists in Revit. Apply default values to start
                StoredVariables.WallSolarAbsorptance.Add(0.6);
                StoredVariables.WallVisibleAbsorptance.Add(0.6);
            }

            //Gather all rooftypes in the document
            foreach (RoofType rooftype in doc.RoofTypes)
            {
                StoredVariables.RoofTypes.Add(rooftype.Name.ToString());
                //Value does not exist in Revit. Apply default value to start
                StoredVariables.RoofRoughness.Add("MediumRough");
                //Try to grab the specified parameter
                try
                {
                    //If Value is 0 default to a standard value of R4
                    if (rooftype.ThermalProperties.ThermalResistance == 0)
                    {
                        StoredVariables.RoofThermalResistance.Add(0.75);
                    }
                    else
                    {
                        StoredVariables.RoofThermalResistance.Add(rooftype.ThermalProperties.ThermalResistance);
                    }
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.RoofThermalResistance.Add(0.75);
                }
                //Try to grab the specified parameter
                try
                {
                    StoredVariables.RoofThermalAbsorptance.Add(rooftype.ThermalProperties.Absorptance);
                }
                //Catch any error and apply a default value
                catch
                {
                    StoredVariables.RoofThermalAbsorptance.Add(0.1);
                }
                //Values do not exists in Revit. Apply default values to start
                StoredVariables.RoofSolarAbsorptance.Add(0.6);
                StoredVariables.RoofVisibleAbsorptance.Add(0.6);
            }
        }      
    }
}